﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthPostAction("ETL", "UpdateDataServiceFolder", "1.0")]
    public class UpdateDataServiceFolderAction : BaseAction<UpdateDataServiceFolderParams, UpdateDataServiceFolderResult>
    {
        protected override ActionResult<UpdateDataServiceFolderResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            UpdateDataServiceFolderParams opParams)
        {
            SER.DtSerService ser = new SER.DtSerService();

            ServiceResult serResult = ser.UpdateFolder(opParams.Id, opParams.Code, opParams.Caption, opParams.ParentId);

            if(serResult.IsSuccess)
            {
                UpdateDataServiceFolderResult result = new UpdateDataServiceFolderResult();

                return new ActionResult<UpdateDataServiceFolderResult>(result);
            }
            else
            {
                return new ActionResult<UpdateDataServiceFolderResult>("1.0", serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }

        private GetDataServiceFolderTreeResult.Item Convert(MOD.MDtSerFolder folder)
        {
            GetDataServiceFolderTreeResult.Item item = new GetDataServiceFolderTreeResult.Item();
            item.Id = folder.Id;
            item.Caption = folder.Caption;
            item.Code = folder.Code;

            foreach (var child in folder.Children)
            {
                item.Items.Add(Convert(child));
            }
            return item;
        }
    }

    public class UpdateDataServiceFolderParams
    {
        [Parameter]
        public Guid Id { get; set; }

        [Parameter]
        public Guid ParentId { get; set; }

        [Parameter]
        public string Code { get; set; }

        [Parameter]
        public string Caption { get; set; }
    }

    public class UpdateDataServiceFolderResult
    {
    }
}
